<template>
    <rui-page :defines="defines">
        <Alert>
            选择批量系统：
            <Select v-model="batchInfos.systemName" style="width:300px" @on-select="loadJobInfo" placeholder="选择批量系统" @on-open-change="queryBatchInfo">
                <Option v-for="batch in batchInfos.batchList" :value="batch.systemName" :key="batch.systemName">{{ batch.systemName }}</Option>
            </Select>
        </Alert>
        <rui-table :defineId="'batchJobInfo'"
                   :fixedLeft="['systemName','batchNo','batchName']"
                   :fixedRight="['action']"
                   :showIndex="false"
                   :select="false"
                   :slots="[{key:'action',slot:'action'}]"
                   @loadDatas="queryBatchJobInfo"
                   :page-able="false"
                   ref="batchJobInfoTable">
            <template slot="toolBar">
                <Button @click="addBatchJobInfo" :disabled="batchInfos.systemName==''" icon="ios-add" type="primary" >新增</Button>
            </template>
            <template slot="action" slot-scope="{ row }">
                <div>
                    <Button @click="editBatchJobInfo(row)" size="small" type="primary">编辑</Button>
                    <Button @click="removeBatchJobInfo(row)" size="small" type="primary">删除</Button>
                </div>
            </template>
        </rui-table>
        <Modal
            :closable="false"
            :mask-closable="false"
            :title="batchJobInfo.configTitle"
            :width="800"
            v-model="batchJobInfo.show">
            <rui-form :form-options="batchJobInfo.options"
                      :label-width="120"
                      :read-only="false"
                      ref="batchJobInfoForm">
                <template  slot="dependJob" slot-scope="{ key,item,data }">
                    <Select v-model="data[key]" style="width:300px" :clearable="true" placeholder="选择批量" @on-open-change="queryJobInfo">
                        <Option v-for="job in jobInfo.jobList" :value="job.batchNo" :key="job.batchNo">{{ job.batchName }}</Option>
                    </Select>
                </template>
            </rui-form>
            <div slot="footer">
                <Button @click="batchJobInfo.show = false" size="small">取消</Button>
                <Button @click="saveBatchJobInfo" size="small" type="primary">保存</Button>
            </div>
        </Modal>
    </rui-page>
</template>

<script>
import {listAllBatchInfo,listJobGroupFilter,editBatchJobInfoApi,removeBatchJobInfoApi} from '_p/basic/api/batch/batchInfo'
import batchJobInfoConfig from './defines/batch-job-info-define'
import {deepClone} from "@/libs/utils/ObjectClone";
export default {
    name: "batch-job-config",
    data() {
        return {
            batchInfos:{
                systemName:'',
                batchList:[]
            },
            jobInfo:{
                ignoreBatchNo:'-',
                jobList:[]
            },
            defines:[{
                id:'batchJobInfo',
                fields:batchJobInfoConfig
            }],
            batchJobInfo:{
                configTitle:'新增批量',
                configType:'add',
                show:false,
                options: [
                    {
                        isGroup: false,
                        grids: [
                            {defineId: 'batchJobInfo', span: 12, fields: ['systemName','batchNo']},
                            {defineId: 'batchJobInfo', span: 12, fields: ['batchName','showOrder']},
                            {defineId: 'batchJobInfo', span: 12, fields: ['dependBatchNo'], slots: [{key: 'dependBatchNo', slotName: 'dependJob'}]},
                        ],
                    }
                ]
            }
        };
    },
    methods: {
        queryBatchJobInfo(queryData) {
            if(this.batchInfos.systemName===''){
                return
            }
            listJobGroupFilter(this.batchInfos.systemName,'-').then(res=>{
                if (res.code === "0000") {
                    this.$refs.batchJobInfoTable.updateTableData(res.data, 100);
                }
            });
        },
        loadJobInfo(v){
            this.batchInfos.systemName = v.value;
            this.refreshBatchJobInfo();
        },
        queryBatchInfo(open){
            if(open){
                listAllBatchInfo().then(res=>{
                    if (res.code === "0000") {
                        this.batchInfos.batchList.splice(0);
                        this.batchInfos.batchList.push(...res.data);
                    }
                });
            }
        },
        queryJobInfo(open){
            if(this.batchInfos.systemName===''){
                return
            }
            if(open){
                listJobGroupFilter(this.batchInfos.systemName,this.jobInfo.ignoreBatchNo).then(res=>{
                    if (res.code === "0000") {
                        this.jobInfo.jobList.splice(0);
                        this.jobInfo.jobList.push(...res.data);
                    }
                });
            }
        },
        refreshBatchJobInfo(){
            this.$refs['batchJobInfoTable'].reloadData();
        },
        addBatchJobInfo(){
            this.batchJobInfo.configTitle = '新建批量信息';
            this.$refs.batchJobInfoForm.setAllFieldRequired(true);
            this.batchJobInfo.configType = 'add';
            this.jobInfo.ignoreBatchNo='-';
            let self = this;
            this.$nextTick(() => {
                self.$refs.batchJobInfoForm.resetFrom();
                this.$refs.batchJobInfoForm.setFieldRequired('dependBatchNo',false);
                this.$refs.batchJobInfoForm.setFiledValue('systemName',this.batchInfos.systemName);
                this.$refs.batchJobInfoForm.setFieldReadOnly('systemName',true);
                this.$refs.batchJobInfoForm.setFieldReadOnly('batchNo',false);
                self.batchJobInfo.show = true;
            })
        },
        editBatchJobInfo(row){
            this.batchJobInfo.configTitle = '维护'+row.systemName+'批量信息';
            this.batchJobInfo.configType = 'modify';
            let self = this;
            self.$refs.batchJobInfoForm.resetFrom();
            this.$refs.batchJobInfoForm.setAllFieldRequired(true);
            this.$refs.batchJobInfoForm.updateFormData(deepClone(row));
            this.jobInfo.ignoreBatchNo=row.batchNo;
            this.queryJobInfo(true);
            this.$nextTick(() => {
                self.batchJobInfo.show = true;
                this.$refs.batchJobInfoForm.setFieldRequired('dependBatchNo',false);
                this.$refs.batchJobInfoForm.setFieldReadOnly('systemName',true);
                this.$refs.batchJobInfoForm.setFieldReadOnly('batchNo',true);
            })
        },
        removeBatchJobInfo(row){
            let self = this;
            this.$Modal.confirm({
                title: "确认",
                content: `确认删除批量[${row.batchName}]？`,
                onOk: () => {
                    removeBatchJobInfoApi(row).then(res => {
                        self.refreshBatchJobInfo();
                    })
                }
            });
        },
        saveBatchJobInfo(){
            let self = this;
            this.$refs.batchJobInfoForm.getForm().validate((valid) => {
                if (valid) {
                    let reqData = self.$refs.batchJobInfoForm.getFormData();
                    editBatchJobInfoApi(reqData).then(res => {
                        if (res.code === '0000') {
                            self.refreshBatchJobInfo();
                            self.batchJobInfo.show = false;
                        } else {
                            self.$Message.error(res.msg);
                        }
                    }).catch(err => {

                    })
                } else {
                    self.batchJobInfo.show = true;
                    self.$Message.error('数据不完整!');
                }
            })
        }
    },
    computed: {

    },
    created(){

    },
    mounted() {
    }
};
</script>
